package com.lanou.connotationjoke.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.LoginActivity;
import com.lanou.connotationjoke.entity.LoginUserEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;


/**
 * Created by dllo on 16/1/8.
 */
public class MsgFragment extends BaseFragment implements View.OnClickListener {
    private View view1;
    private View view2;
    private View view3;
    private NetworkImageView loginIv;
    private SharedPreferences sharedPreferences;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, null);
        loginIv = (NetworkImageView) view.findViewById(R.id.iv_msg_to_login);
        view1 = view.findViewById(R.id.layout_msg_contribute);
        view2 = view.findViewById(R.id.layout_msg_system);
        view3 = view.findViewById(R.id.layout_msg_friends);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);


        sharedPreferences = getActivity().getSharedPreferences("userData", Context.MODE_PRIVATE);
        String iconUrl = sharedPreferences.getString("icon","");
        loginIv.setDefaultImageResId(R.mipmap.ic_discovery_default_avatar);
        loginIv.setErrorImageResId(R.mipmap.ic_discovery_default_avatar);
        loginIv.setImageUrl(iconUrl, VolleySingleton.getImageLoader());



        view1.setOnClickListener(this);
        view2.setOnClickListener(this);
        view3.setOnClickListener(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(LoginUserEty loginUserEty) {
        loginIv.setImageUrl(loginUserEty.getIconUrl(), VolleySingleton.getImageLoader());
    }



    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.layout_msg_contribute
                ||v.getId()==R.id.layout_msg_system
                ||v.getId()==R.id.layout_msg_friends){
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }
    }
}
